﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.International.Converters.PinYinConverter;//引用命名空间

namespace MyTest
{
    /// <summary>
    /// 教员:薛海
    /// 微信:xuehai03
    /// QQ:289477003
    /// 汉字转换成拼音
    /// 2019-2-20
    /// </summary>
    public partial class FrmPingYin : Form
    {
        public FrmPingYin()
        {
            InitializeComponent();
        }


        //汉字转化为拼音
        public string GetPingYin(string str)
        {
            string s = string.Empty;
            foreach (char item in str)
            {
                try
                {
                    ChineseChar china = new ChineseChar(item);
                    string r = china.Pinyins[0].ToString();
                    s += r.Substring(0, r.Length - 1);
                }
                catch (Exception)
                {

                    s += item.ToString();
                }

            }
            return s;
        }

        //汉字转化为拼音首字母
        public string getFirstPingYin(string str)
        {
            string r = string.Empty;

            foreach (char item in str)
            {
                try
                {
                    ChineseChar china = new ChineseChar(item);
                    string t = china.Pinyins[0].ToString();

                    r += t.Substring(0, 1);
                }
                catch (Exception)
                {

                    r += item.ToString();
                }
            }
            return r;
        }

        private void btnPingYin_Click(object sender, EventArgs e)
        {
            string str = this.txtChinses.Text.Trim();
            string result = GetPingYin(str);

            this.txtPinYin.Text = result;
        }

        //生成首字母
        private void btnFirstPinYin_Click(object sender, EventArgs e)
        {
            string str = this.txtChinses.Text.Trim();
            string result = this.getFirstPingYin(str);

            this.txtPinYin.Text = result;
        }

    }
}
